//
//  YYHomeVC.m
//  Project
//
//  Created by Yown on 2024/1/15.
//

#import "YYHomeVC.h"
#import "YYHomeView.h"
#import "YYHomeModel.h"

#import <BINetworkModule/BINetworkManager.h>
#import <BINetworkModule/BICookieManager.h>
#import <BIBluetoothModule/BIBluetoothManager.h>
#import <BIToolModule/BIProjectDefine.h>

#import <BIProjectModule/BIAppDefine.h>
#import <BIToolModule/NSString+Device.h>

#import <Masonry/Masonry.h>

@interface YYHomeVC ()

@property (nonatomic) YYHomeModel *homeModel;
@property (nonatomic) YYHomeView *homeView;

@end

@implementation YYHomeVC

#pragma mark - Life Cycle

+ (void)load {
    [[BIRouteManager sharedInstance] registerCMD:@"home" objectOrClass:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupDatas];
    [self setupViews];
    [self setupLayouts];
    [self setupNotification];
}

- (void)setupDatas {
    self.homeModel = [YYHomeModel new];    
}

- (void)setupViews {
    self.yy_navigationBar.isHiddenNaviBar = YES;
    
    self.homeView = [YYHomeView view];
    @weakify(self);
    self.homeView.clickResetBlock = ^{
        @strongify(self);
        [self yy_sendAllData];
        [self yy_refreshDeviceView];
    };
    [self.view addSubview:self.homeView];
    
    [self yy_refreshDeviceView];
}

- (void)setupLayouts {
    [self.homeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
}

- (void)setupNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshBluetoothState:) name:kBluetoothConnectResultNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(readNotifyFinish:) name:kBluetoothNotifyFinishNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(readBluetoothData:) name:kBluetoothSendDataNotification object:nil];
}

#pragma mark - Notification

- (void)refreshBluetoothState:(NSNotification *)notification {
    BIBluetoothModel *model = notification.object;
    if (model.state == kBIBluetoothConnected) {
        // 蓝牙连接上，存储mac地址到cookie
        [[BICookieManager sharedInstance] setCookieName:@"mac" cookieValue:model.macAddress isUser:NO];
        [self requestHardwareInfo];
    }
    [self yy_refreshDeviceView];
}

- (void)readNotifyFinish:(NSNotification *)notification {
    // 获取蓝牙端配置
    [self yy_sendAllData];
    [self yy_refreshDeviceView];
}

- (void)readBluetoothData:(NSNotification *)notification {
    BIBluetoothModel *model = notification.object;
    [self.homeView reloadBleModel:model];
}

#pragma mark - Private Method

- (void)yy_refreshDeviceView {
    __block BIBluetoothModel *model = nil;
    [[BIBluetoothManager sharedInstance].allPeripherals enumerateObjectsUsingBlock:^(BIBluetoothModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.state == kBIBluetoothConnecting) {
            model = obj;
        } else if (obj.state == kBIBluetoothConnected) {
            model = obj;
            *stop = YES;
        }
    }];

    [self.homeView reloadBleModel:model];
    [self.homeView reloadHomeModel:self.homeModel];
}

- (void)requestHardwareInfo {
    NSString *path = @"/api/app/project/getInfo";
    BINetworkRequest *request = [[BINetworkManager sharedInstance] requestWithPath:path arguments:nil succeedBlock:^(BINetworkResponse *response) {
        [BIBluetoothManager sharedInstance].currentBluetoothModel.deviceImageURL = response.modelValue[@"imageUrl"];
        [self yy_refreshDeviceView];
    } failureBlock:nil completionBlock:nil];
    request.methodType = kBINetworkGetMethodType;
    [[BINetworkManager sharedInstance] startRequest:request];
}

- (void)yy_sendAllData {
    BIBluetoothModel *model = [BIBluetoothManager sharedInstance].currentBluetoothModel;
    NSData *data = [model.cmdModel getHardwareConfig];
    [[BIBluetoothManager sharedInstance] sendData:data model:model];
}

#pragma mark - Replace Super Method

- (void)languageChanged:(NSNotification *)notification {
    [self.homeView reloadUIWithLanguageChanged];
}

@end
